package com.le.edu.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.le.edu.domain.entity.Course;
import com.le.edu.domain.screen.MonthVO;
import com.le.edu.domain.screen.PieVO;
import com.le.edu.domain.screen.SexVO;
import com.le.edu.domain.vo.ScreenVO;
import com.le.edu.resp.Result;
import com.le.edu.service.CourseService;
import com.le.edu.service.SectionService;
import com.le.edu.service.SubjectService;
import com.le.edu.service.UserService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author Le
 * @create 2024-04-30 12:56
 */
@RestController
@RequiredArgsConstructor
@Tag(name = "数据大屏接口")
@RequestMapping("/screen")
public class ScreenController {
    private final UserService userService;
    private final CourseService courseService;
    private final SubjectService subjectService;
    private final SectionService sectionService;
    private final ThreadPoolExecutor executor;

    @GetMapping
    public Result<ScreenVO> screen() {
        ScreenVO vo = new ScreenVO();
        //计算 男女比例
        CompletableFuture<Void> userFuture = CompletableFuture.runAsync(() -> {
            SexVO sexVO = userService.computeSexRatio();
            BeanUtil.copyProperties(sexVO, vo);
        }, executor);

        //计算 分类对应的课程数量 分类的子分类数
        CompletableFuture<Void> subjectFuture = CompletableFuture.runAsync(() -> {
            List<PieVO> pieVOS = subjectService.computeCourseCountBySubject();
            vo.setSubjectPie(pieVOS);

            List<PieVO> subjectCounts = subjectService.computeSubjectCount();
            List<String> nameList = subjectCounts.stream()
                    .map(PieVO::getName)
                    .toList();
            List<Integer> valueList = subjectCounts.stream()
                    .map(PieVO::getValue)
                    .toList();
            vo.getParentSubjectCountMap().put("xData", nameList);
            vo.getParentSubjectCountMap().put("yData", valueList);
        }, executor);

        // 计算 课程
        CompletableFuture<Void> courseFuture = CompletableFuture.runAsync(() -> {
            List<Course> courseList = courseService.list(new LambdaQueryWrapper<Course>()
                    .orderByDesc(Course::getViewCount)
                    .orderByDesc(Course::getCreateTime)
                    .last("limit 7"));
            List<String> titleList = courseList.stream()
                    .map(Course::getTitle)
                    .toList();
            List<Long> viewCountList = courseList.stream()
                    .map(Course::getViewCount)
                    .toList();
            vo.getCourseTitleViewCountMap().put("xData", titleList);
            vo.getCourseTitleViewCountMap().put("yData", viewCountList);
        }, executor);

        //计算 课时
        CompletableFuture<Void> sectionFuture = CompletableFuture.runAsync(() -> {
            List<MonthVO> monthVOS = sectionService.computeMonthCount();
            List<String> monthList = monthVOS.stream()
                    .map(MonthVO::getMonth)
                    .toList();
            List<Integer> countList = monthVOS.stream()
                    .map(MonthVO::getCount)
                    .toList();
            vo.getSectionDateCountMap().put("xData", monthList);
            vo.getSectionDateCountMap().put("yData", countList);
        }, executor);

        CompletableFuture.allOf(userFuture, subjectFuture, courseFuture, sectionFuture).join();
        return Result.ok(vo);
    }
}
